﻿using UnityEngine;
using System.Collections;

public class ShootEggsScript : MonoBehaviour {

	float xMinimum = 0f;
	float xMaximum = 0f;
	float yStart = 0f;
	//float yEnd = 0f;
	public double TimerInterval;
	public double EnemyTimerInterval;
	public double HeartTimerInterval;
	public float maxXForce = 0f;
	public float minXForce = 0f;
	public float minGravity = 0f;
	public float maxGravity = 0f;
	public Transform eggObject;
	public Transform enemyObject;
	public Transform heartObject;
	GameObject instantiatedObjects;
	bool ReadyToShoot = false;
	bool ReadyToShootEnemy = false;
	bool ReadyToShootHeart = false;
	double eggInterval = 0f;
	double enemyInterval = 0f;
	double heartInterval = 0f;
	Camera cam;

	// Use this for initialization
	void Start () {
		this.Reset ();
	}

	public void Reset() {
		int width = Screen.width;
		int height = Screen.height;
		height -= 0;
		cam = (Camera)GameObject.Find ("Main Camera").GetComponent(typeof(Camera));
		Vector3 result = cam.ScreenToWorldPoint (new Vector3 (width, height, 0f));
		float worldX = result.x;
		float worldY = result.y;		
		this.xMinimum = -1f * (worldX * 0.8f);
		this.xMaximum = worldX * 0.8f;
		this.yStart = worldY;

		this.instantiatedObjects = GameObject.Find ("InstantiatedObjects");
		//this.yEnd = -1f * worldY;
		
		this.eggInterval = this.TimerInterval;
		this.enemyInterval = this.EnemyTimerInterval;
		this.heartInterval = this.HeartTimerInterval;

		GameMaintainerSingleton.Instance.CloseAds();

		//GameObject.DontDestroyOnLoad (this.eggObject.gameObject);
		//GameObject.DontDestroyOnLoad (this.enemyObject.gameObject);
		//GameObject.DontDestroyOnLoad (this.heartObject.gameObject);
	}

	// Update is called once per frame
	void Update () {
		eggInterval -= Time.deltaTime;
		enemyInterval -= Time.deltaTime;
		heartInterval -= Time.deltaTime;
		if (eggInterval < 0f) {
			this.ReadyToShoot = true;
			this.eggInterval = this.TimerInterval;
		}

		if (enemyInterval < 0f) {
			this.ReadyToShootEnemy = true;
			this.enemyInterval = this.EnemyTimerInterval;
		}

		if (heartInterval < 0f) {
			this.ReadyToShootHeart = true;
			this.heartInterval = this.HeartTimerInterval;
		}

		if (ReadyToShoot) {
			this.ReadyToShoot = false;

			float xForce = UnityEngine.Random.Range (minXForce, maxXForce);
			float gravity = UnityEngine.Random.Range (minGravity, maxGravity);
			float xStart = UnityEngine.Random.Range (xMinimum, xMaximum);
			
			Vector3 startingPoint = new Vector3 (xStart, yStart, 0f);
			
			GameObject instantiatedEgg = (GameObject)Instantiate(this.eggObject.gameObject,startingPoint,Quaternion.identity);
			instantiatedEgg.transform.parent = this.instantiatedObjects.transform;
			LifeCountDown cDown = (LifeCountDown)instantiatedEgg.GetComponent(typeof(LifeCountDown));
			cDown.LifeActivated = true;
			Rigidbody2D rBody = (Rigidbody2D)instantiatedEgg.GetComponent (typeof(Rigidbody2D));
			rBody.gravityScale = gravity;
			rBody.AddForce (new Vector2 (xForce, 0f));

		}

		if (ReadyToShootEnemy) {
			this.ReadyToShootEnemy = false;
			
			float xForce = UnityEngine.Random.Range (minXForce, maxXForce);
			float gravity = UnityEngine.Random.Range (minGravity, maxGravity);
			float xStart = UnityEngine.Random.Range (xMinimum, xMaximum);
			
			Vector3 startingPoint = new Vector3 (xStart, yStart, 0f);
			
			GameObject instantiatedEnemy = (GameObject)Instantiate(this.enemyObject.gameObject,startingPoint,Quaternion.identity);
			instantiatedEnemy.transform.parent = this.instantiatedObjects.transform;
			LifeCountDown cDown = (LifeCountDown)instantiatedEnemy.GetComponent(typeof(LifeCountDown));
			cDown.LifeActivated = true;
			Rigidbody2D rBody = (Rigidbody2D)instantiatedEnemy.GetComponent (typeof(Rigidbody2D));
			rBody.gravityScale = gravity;
			rBody.AddForce (new Vector2 (xForce, 0f));

			if (xForce > 0f) {
				Vector3 vec = rBody.gameObject.transform.localScale;
				vec.x *= -1f;
				rBody.gameObject.transform.localScale = vec;
			}
		}

		if (ReadyToShootHeart) {
			this.ReadyToShootHeart = false;
			
			float xForce = UnityEngine.Random.Range (minXForce, maxXForce);
			float gravity = UnityEngine.Random.Range (minGravity, maxGravity);
			float xStart = UnityEngine.Random.Range (xMinimum, xMaximum);
			
			Vector3 startingPoint = new Vector3 (xStart, yStart, 0f);
			
			GameObject instantiatedHeart = (GameObject)Instantiate(this.heartObject.gameObject,startingPoint,Quaternion.identity);
			instantiatedHeart.transform.parent = this.instantiatedObjects.transform;
			LifeCountDown cDown = (LifeCountDown)instantiatedHeart.GetComponent(typeof(LifeCountDown));
			cDown.LifeActivated = true;
			Rigidbody2D rBody = (Rigidbody2D)instantiatedHeart.GetComponent (typeof(Rigidbody2D));
			rBody.gravityScale = gravity;
			rBody.AddForce (new Vector2 (xForce, 0f));
		}
	}
}
